using System;
using System.IO;
using System.Net;

namespace ThatsMeSDK.Core
{
	/// <summary>
	/// Virtual Browser for Post and Get requests
	/// Works with Silverlight, MonoDroid and MonoTouch
	/// </summary>
	internal class VBrowser
	{
		/// <summary>
		/// Browser Timout
		/// </summary>
		internal Int32 TimeOut { get; private set; }
        internal event LoadStringCompleted LoadStringCompleted;

        ServiceHandlerMethod callEvent;
        object tagStorage;

		/// <summary>
		/// Creates a new VBrowser
		/// </summary>
		/// <param name="TimeOut">Connection Timeout</param>
		internal VBrowser(int TimeOut)
		{
			this.TimeOut = TimeOut;
		}

		/// <summary>
		/// Get Web Request
		/// </summary>
		/// <param name="uri">Content Uri</param>
		/// <returns>Returns the content of the requestet Uri</returns>
        internal void GetRequest(Uri uri, ServiceHandlerMethod c, object Tag)
		{
            callEvent = c;
            tagStorage = Tag;

            WebClient req = new WebClient();
            req.DownloadStringCompleted +=new DownloadStringCompletedEventHandler(req_DownloadStringCompleted);

			req.DownloadStringAsync(uri);
		}

        void  req_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            this.LoadStringCompleted(this, new LoadStringCompletedEventArgs(e.Result), this.callEvent, tagStorage);
        }
	}
}
